package com.kldy8.kldyw.filmfactory.dteam;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kldy8.kldyw.MyWebActivity;
import com.kldy8.kldyw.R;
import com.kldy8.kldyw.filmfactory.dteam.bean.Dteam;
import com.kldy8.kldyw.filmfactory.dteam.bean.Roles;
import com.kldy8.kldyw.system.UrlBuilder;
import com.kldy8.kldyw.web.AppWebUrl;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;


/**
 *
 * 人才
 * Created by Administrator on 2016/9/5.
 */
public class DteamAdapter extends BaseAdapter
{
    ImageLoader loader;

    private List<Dteam> listItems;
    private Context context;                        //运行上下文
    private LayoutInflater listContainer;           //视图容器
    private boolean[] hasChecked;

    //记录商品选中状态
    public final class ListItemView
    {                //自定义控件集合
        public ImageView image;
        public TextView works_name;
        public TextView works_type;

        public RelativeLayout relativeLayout;
        public ImageView sex_img;
        public TextView name_tv;
        public TextView type_tv;
        public TextView participant_tv;

        public RelativeLayout relativeLayout2;
        public ImageView sex_img_ii;
        public TextView name_tv_ii;
        public TextView type_tv_ii;
        public TextView participant_tv_ii;

        public RelativeLayout relativeLayout3;
        public ImageView sex_img_iii;
        public TextView name_tv_iii;
        public TextView type_tv_iii;
        public TextView participant_tv_iii;

    }

    public void replaceOrAdd(boolean isInit, List<Dteam> listItems)
    {
        if (isInit)
        {
            this.listItems = listItems;
        }
        else
        {
            if (this.listItems != null)
            {
                if (listItems != null)
                {
                    this.listItems.addAll(listItems);
                }
            }
            else
            {
                this.listItems = listItems;
            }
        }
    }

    public DteamAdapter(Context context, List<Dteam> filmTalents)
    {
        this.context = context;
        this.listItems = filmTalents;
        listContainer = LayoutInflater.from(context);   //创建视图容器并设置上下文
        hasChecked = new boolean[getCount()];
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .build();
        loader = ImageLoader.getInstance();
        loader.init(config);
    }

    public int getCount()
    {
        // TODO Auto-generated method stub
        if (null == listItems )
        {
             return 1;
        }
        else
        {
            return listItems.size();
        }
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    /**
     * 记录勾选了哪个物品
     * @param checkedID 选中的物品序号
     */
    private void checkedChange(int checkedID) {
        hasChecked[checkedID] = !hasChecked[checkedID];
    }

    /**
     * 判断物品是否选择
     * @param checkedID 物品序号
     * @return 返回是否选中状态
     */
    public boolean hasChecked(int checkedID) {
        return hasChecked[checkedID];
    }



    /**
     * ListView Item设置
     */
    public View getView(final int position, View convertView, ViewGroup parent)
    {

        // TODO Auto-generated method stub
        final int selectID = position;
        //自定义视图
        ListItemView  listItemView = null;
        if (null == listItems || listItems.size() == 0)
        {
            convertView = listContainer.inflate(R.layout.view_notfount, null);
            return convertView;
        }
        else{
        if (convertView == null)
        {

            listItemView = new ListItemView();
            //获取list_item布局文件的视图
            convertView = listContainer.inflate(R.layout.dteam_item, null);
            //获取控件对象
            listItemView.image = (ImageView) convertView.findViewById(R.id.image);
            listItemView.image = (ImageView) convertView.findViewById(R.id.image);
            listItemView.works_name = (TextView) convertView.findViewById(R.id.works_name);
            listItemView.works_type = (TextView) convertView.findViewById(R.id.works_type);

            listItemView.relativeLayout = (RelativeLayout) convertView.findViewById(R.id.rl_1);
            listItemView.sex_img = (ImageView) convertView.findViewById(R.id.sex_img);
            listItemView.name_tv = (TextView)convertView.findViewById(R.id.name_tv);
            listItemView.type_tv = (TextView)convertView.findViewById(R.id.type_tv);
            listItemView.participant_tv = (TextView)convertView.findViewById(R.id.participant_tv);

            listItemView.relativeLayout2 = (RelativeLayout) convertView.findViewById(R.id.rl_2);
            listItemView.sex_img_ii = (ImageView) convertView.findViewById(R.id.sex_img_ii);
            listItemView.name_tv_ii = (TextView)convertView.findViewById(R.id.name_tv_ii);
            listItemView.type_tv_ii = (TextView)convertView.findViewById(R.id.type_tv_ii);
            listItemView.participant_tv_ii = (TextView)convertView.findViewById(R.id.participant_tv_ii);

            listItemView.relativeLayout3 = (RelativeLayout) convertView.findViewById(R.id.rl_3);
            listItemView.sex_img_iii = (ImageView) convertView.findViewById(R.id.sex_img_iii);
            listItemView.name_tv_iii = (TextView)convertView.findViewById(R.id.name_tv_iii);
            listItemView.type_tv_iii = (TextView)convertView.findViewById(R.id.type_tv_iii);
            listItemView.participant_tv_iii = (TextView)convertView.findViewById(R.id.participant_tv_iii);

            //设置控件集到convertView
            convertView.setTag(listItemView);
        }else {
            listItemView = (ListItemView)convertView.getTag();
        }

        Dteam  dteam = listItems.get(position);
        String headUrl = ""+dteam.image;
        loader.displayImage(headUrl, listItemView.image);
        listItemView.works_name.setText(dteam.works_name);
        listItemView.works_type.setText(dteam.works_type);

        List<Roles>  rolesList = dteam.rolesList;

        if (rolesList != null && rolesList.size() > 0)
        {
            if (rolesList.size()>0)
            {
                listItemView.relativeLayout.setVisibility(View.VISIBLE);
                listItemView.relativeLayout2.setVisibility(View.GONE);
                listItemView.relativeLayout3.setVisibility(View.GONE);

                Roles roles =  rolesList.get(0);
                if ("0".equals(roles.gender))
                {
                    listItemView.sex_img.setImageResource(R.drawable.icon_male_dtame);
                }
                else
                {
                    listItemView.sex_img.setImageResource(R.drawable.icon_female_dtame);
                }
                listItemView.name_tv.setText(roles.name);
                listItemView.type_tv.setText("("+roles.type+")");
                listItemView.participant_tv.setText(roles.participant+context.getResources().getString(R.string.rbm));
            }
            if (rolesList.size()>1)
            {
                listItemView.relativeLayout2.setVisibility(View.VISIBLE);
                Roles roles =  rolesList.get(1);
                if ("0".equals(roles.gender)) {
                    listItemView.sex_img_ii.setImageResource(R.drawable.icon_male_dtame);
                }
                else
                {
                    listItemView.sex_img_ii.setImageResource(R.drawable.icon_female_dtame);
                }
                listItemView.name_tv_ii.setText(roles.name);
                listItemView.type_tv_ii.setText("("+roles.type+")");

                listItemView.participant_tv_ii.setText(roles.participant+context.getResources().getString(R.string.rbm));
            }
            if (rolesList.size()>2)
            {
                Roles roles =  rolesList.get(2);
                listItemView.relativeLayout3.setVisibility(View.VISIBLE);
                if ("0".equals(roles.gender))
                {
                    listItemView.sex_img_iii.setImageResource(R.drawable.icon_male_dtame);
                }
                else
                {
                    listItemView.sex_img_iii.setImageResource(R.drawable.icon_female_dtame);
                }
                listItemView.name_tv_iii.setText(roles.name);
                listItemView.type_tv_iii.setText("("+roles.type+")");
                listItemView.participant_tv_iii.setText(roles.participant+context.getResources().getString(R.string.rbm));
            }
        }
            else
        {
            listItemView.relativeLayout.setVisibility(View.GONE);
            listItemView.relativeLayout2.setVisibility(View.GONE);
            listItemView.relativeLayout3.setVisibility(View.GONE);

        }

        convertView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                Dteam mdteam = listItems.get(position);
                Intent intent = new Intent();
                String resultUrl = AppWebUrl.createUrl(mdteam.url);
                intent.putExtra("url",resultUrl);
                intent.setClass(context, MyWebActivity.class);
                ((Activity)context).startActivity(intent);
                ((Activity)context).overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
            }
        });

        return convertView;
    }
    }
}